package io.renren.modules.sys.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysDictEntity;
import io.renren.modules.sys.service.SysDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2025-03-30 22:33:52
 */
@RestController
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysDictService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public R info(@PathVariable("id") Integer id) {
        SysDictEntity sysDict = sysDictService.getById(id);

        return R.ok().put("sysDict", sysDict);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public R save(@RequestBody SysDictEntity sysDict) {
        sysDictService.save(sysDict);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public R update(@RequestBody SysDictEntity sysDict) {
        sysDictService.updateById(sysDict);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public R delete(@RequestBody Integer[] ids) {
        sysDictService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据类别查询字典列表
     */
    @GetMapping("/list/{type}")
    public R listByType(@PathVariable String type) {
        List<SysDictEntity> dictList = sysDictService.getListByType(type);
        return R.ok().put("dictList", dictList);
    }

    /**
     * 获取所有类别
     */
    @GetMapping("/type/list")
    @RequiresPermissions("sys:dict:list")
    public R typeList() {
        List<String> typeList = sysDictService.getTypeList();
        return R.ok().put("dictTypeList", typeList);
    }


}
